package android.app.gfestival.soap;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public class SoapParser {
//	private static final String METHOD_NAME = "TopGoalScorers";
//	 private static final String SOAP_ACTION = "http://footballpool.dataaccess.eu/data/TopGoalScorers";
//	 private static final String NAMESPACE = "http://footballpool.dataaccess.eu";
//	 private static final String URL = "http://footballpool.dataaccess.eu/data/info.wso?WSDL";
	private static final String METHOD_NAME = "get_events_by_day_id";
	 private static final String SOAP_ACTION = "http://www.halloapp.net/soap#get_events_by_day_id";
	 private static final String NAMESPACE = "http://www.halloapp.net/soap";
//	 private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
	 private static final String URL = "http://www.halloapp.net/soap";
	//you can get these values from the wsdl file^
	private static final String TAG = "SOAP";
	 
	 public SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL) throws IOException, XmlPullParserException 
	 {
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request
//	    request.addProperty("iTopN", ""); //variable name, value. I got the variable name, from the wsdl file!
	    request.addProperty("event_day_id", 5); //test
	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
	    envelope.setOutputSoapObject(request);  //prepare request
	    AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
	    httpTransport.debug = true;  //this is optional, use it if you don't want to use a packet sniffer to check what the sent 
	                                 //message was (httpTransport.requestDump)
	    httpTransport.call(SOAP_ACTION, envelope); //send request
	    SoapObject result=(SoapObject)envelope.getResponse(); //get response
	    return result;
	 }
	 
	 public void requestAllEventById(int id){
		 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request
		 try {
//		    request.addProperty("iTopN", ""); //variable name, value. I got the variable name, from the wsdl file!
		    request.addProperty("event_day_id", "5"); //test
		    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
		    envelope.setOutputSoapObject(request);  //prepare request
//		    AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);  
//		    httpTransport.debug = true;  //this is optional, use it if you don't want to use a packet sniffer to check what the sent 
		                                 //message was (httpTransport.requestDump)
		    envelope.dotNet = true;
		    HttpTransportSE se = new HttpTransportSE(URL);
		    se.debug = true;
		    se.call(SOAP_ACTION, envelope); //send request
//		    SoapPrimitive primitive = (SoapPrimitive) envelope.getResponse();
//		    Log.i("PARSER" , "---->" + primitive);
		    Log.i("PARSER" , "---->" + envelope.bodyIn);
			SoapObject result=(SoapObject)envelope.bodyIn;
			Event event = new Event();
			
			Ksoap2ResultParser.parseBusinessObject(result.getProperty(0).toString(), event);
			Log.i("PARSER" , "---->" + event.event_description + event.event_day_id);
			} catch (SoapFault e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} //get response
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	 }
	 
	//usage:
	//SoapObject result=soap(METHOD_NAME, SOAP_ACTION, NAMESPACE, URL);
	//don't forget to catch the exceptions!!
	 public void test(){
		 try {
			 
			 SoapObject soapresultmsg = soap(METHOD_NAME, SOAP_ACTION, NAMESPACE, URL);
			 int length = soapresultmsg.getPropertyCount();
			 for(int i = 0; i < length; i++){
				 TopGoalScores topGoalScores=new TopGoalScores();
				 Ksoap2ResultParser.parseBusinessObject(soapresultmsg.getProperty(i).toString(), topGoalScores);
				 Log.i(TAG, "---->" + topGoalScores.toString());
			 }
			 
		 } catch (NumberFormatException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		 } catch (IllegalArgumentException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		 } catch (IllegalAccessException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		 } catch (InstantiationException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		 } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
}

